package com.jess.arms.database;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jess.arms.base.delegate.sys.LoginReponsBody;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Response;

/**
 * FileName: MMkvHelper
 * Created by zlx on 2020/9/21 14:40
 * Email: 1170762202@qq.com
 * Description:
 */
public class MMkvHelper {
    private final Gson mGson = new Gson();

    private static MMKV mmkv;

    private MMkvHelper() {
        mmkv = MMKV.defaultMMKV();
    }

    public static MMkvHelper getInstance() {
        return MMkvHolder.INSTANCE;
    }

    private static class MMkvHolder {
        private static final MMkvHelper INSTANCE = new MMkvHelper();
    }


    /**
     * 保存用户信息
     */
//    public void saveUserInfo(UserInfo userInfo) {
//        mmkv.encode(C.USER_INFO, userInfo);
//    }
    public void saveUserInfo(LoginReponsBody loginReponsBody) {
        mmkv.encode(C.USER_INFO, loginReponsBody);
    }

    public LoginReponsBody getUserInfo() {
        return mmkv.decodeParcelable(C.USER_INFO, LoginReponsBody.class);
    }

    public void saveLanguage(Locale locale) {
        mmkv.encode(C.LANGUAGE, JSON.toJSONString(locale));
    }

    public Locale getLanguage() {
        String s = mmkv.decodeString(C.LANGUAGE);
        if (TextUtils.isEmpty(s)){
            return null;
        }
        try {
            Locale locale = JSON.parseObject(s, Locale.class);
            return locale;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public <T> void saveProjectTabs(List<T> dataList) {
        saveList(C.PROJECT_TABS, dataList);
    }

    public <T> List<T> getProjectTabs(Class<T> cls) {
        return getTList(C.PROJECT_TABS, cls);
    }

    /**
     * 保存list
     */
    private <T> void saveList(String tag, List<T> dataList) {
        if (null == dataList || dataList.size() <= 0)
            return;
        //转换成json数据，再保存
        String strJson = mGson.toJson(dataList);
        mmkv.encode(tag, strJson);
    }

    private <T> List<T> getTList(String tag, Class<T> cls) {
        List<T> dataList = new ArrayList<>();
        String strJson = mmkv.decodeString(tag, null);
        if (null == strJson) {
            return dataList;
        }
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                dataList.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataList;
    }

    public void logout() {
        LogUtils.i("logout");
        mmkv.remove(C.USER_INFO);
    }

    /**
     * 保存常量
     * @param tag 标识
     * @param str 对象
     */
    public void saveString(String tag,String str){
        mmkv.encode(tag, str);
    }

    /**
     * 移除常量
     * @param tag 标识
     */
    public void removeString(String tag){
        mmkv.remove(tag);
    }

    /**
     * 获取常量
     * @param tag 标识
     */
    public String getString(String tag){
        return mmkv.decodeString(tag);
    }

    /**
     * 保存 baseUrl
     * @param str baseUrl
     */
    public void saveBaseUrl(String str){
        mmkv.encode(BaseConstant.BASE_URL, str);
    }

    /**
     * 获取 baseUrl
     * @return baseUrl
     */
    public String getBaseUrl(){
        return mmkv.decodeString(BaseConstant.BASE_URL);
    }

    /**
     * 移除 baseUrl
     */
    public void removeBaseUrl(){
        mmkv.remove(BaseConstant.BASE_URL);
    }

    /**
     * 保存登录url
     * @param url
     */
    public void saveLoginUrl(String url){
        mmkv.encode(BaseConstant.LOGIN_URL, url);
    }


    public String getLoginUrl(){
        return mmkv.decodeString(BaseConstant.LOGIN_URL);
    }


    public void removeLoginUrl(){
        mmkv.remove(BaseConstant.LOGIN_URL);
    }

    /**
     * 保存登录成功跳转地址
     * @param url
     */
    public void saveMainUrl(String url){
        mmkv.encode(BaseConstant.MAIN_URL, url);
    }
    public String getMainUrl(){
        return mmkv.decodeString(BaseConstant.MAIN_URL);
    }
    public void removeMainUrl(){
        mmkv.remove(BaseConstant.MAIN_URL);
    }

    /**
     * 保存公司名称
     * @param name
     */
    public void saveCompanyName(String name){
        mmkv.encode(BaseConstant.COMPANY_NAME, name);
    }
    public String getCompanyName(){
        return mmkv.decodeString(BaseConstant.COMPANY_NAME);
    }
    public void removeCompanyName(){
        mmkv.remove(BaseConstant.COMPANY_NAME);
    }

    /**
     * 保存版本号
     * @param appVersion
     */
    public void saveAppVersion(String appVersion){
        mmkv.encode(BaseConstant.APP_VERSION, appVersion);
    }
    public String getAppVersion(){
        return mmkv.decodeString(BaseConstant.APP_VERSION);
    }
    public void removeAppVersion(){
        mmkv.remove(BaseConstant.APP_VERSION);
    }


    /**
     * 保存服务起名字
     * @param url
     */
    public void saveServiceName(String url){
        mmkv.encode(BaseConstant.SERVICE_NAME, url);
    }
    public String getServiceName(){
        return mmkv.decodeString(BaseConstant.SERVICE_NAME);
    }
    public void removeServiceName(){
        mmkv.remove(BaseConstant.SERVICE_NAME);
    }

    /**
     * 保存WebsocketUrl
     * @param url
     */
    public void saveWebsocketUrl(String url){
        mmkv.encode(BaseConstant.WEBSOKET_URL, url);
    }
    public String getWebsocketUrl(){
        return mmkv.decodeString(BaseConstant.WEBSOKET_URL);
    }
    public void removeWebsocketUrl(){
        mmkv.remove(BaseConstant.WEBSOKET_URL);
    }

    /**
     * 保存app名称
     * @param name
     */
    public void saveAppName(String name){
        mmkv.encode(BaseConstant.APP_NAME, name);
    }
    public String getAppName(){
        return mmkv.decodeString(BaseConstant.APP_NAME);
    }
    public void removeAppName(){
        mmkv.remove(BaseConstant.APP_NAME);
    }

    /**
     * 保存app logo
     * @param logoIconPath
     */
    public void saveAppLogo(int logoIconPath){
        mmkv.encode(BaseConstant.APP_LOGO, logoIconPath);
    }
    public int getAppLogo(){
        return mmkv.decodeInt(BaseConstant.APP_LOGO);
    }
    public void removeAppLogo(){
        mmkv.remove(BaseConstant.APP_LOGO);
    }

    /**
     * 保存WEBSOKET可见性
     * @param isVisible
     */
    public void webSocketIsVisible(boolean isVisible){
        mmkv.encode(BaseConstant.WEBSOKET_VISIBLE, isVisible);
    }
    public boolean getWebSocketVisible(){
        return mmkv.decodeBool(BaseConstant.WEBSOKET_VISIBLE);
    }
    public void removeWebSocketVisible(){
        mmkv.remove(BaseConstant.WEBSOKET_VISIBLE);
    }

    /**
     * 保存app是否强制更新
     * @param compulsiveUpdate
     */
    public void appIscompulsiveUpdate(boolean compulsiveUpdate){
        mmkv.encode(BaseConstant.COMPULSIVE_UPDATE, compulsiveUpdate);
    }
    public boolean getIscompulsiveUpdate(){
        return mmkv.decodeBool(BaseConstant.COMPULSIVE_UPDATE);
    }
    public void removeIscompulsiveUpdate(){
        mmkv.remove(BaseConstant.COMPULSIVE_UPDATE);
    }

    /**
     * 保存 app更新的Url
     * @param str AppUpdateUrl
     */
    public void saveAppUpdateUrl(String str){
        mmkv.encode(BaseConstant.APP_UPDATE_URL, str);
    }

    /**
     * 获取 AppUpdateUrl
     * @return AppUpdateUrl
     */
    public String getAppUpdateUrl(){
        return mmkv.decodeString(BaseConstant.APP_UPDATE_URL);
    }

    /**
     * 移除 AppUpdateUrl
     */
    public void removeAppUpdateUrl(){
        mmkv.remove(BaseConstant.APP_UPDATE_URL);
    }


    /**
     * 启用离线模式
     * @param offLine
     */
    public void offLineMode(boolean offLine){
        mmkv.encode(BaseConstant.OFFLINE_MODE, offLine);
    }
    public boolean getOffLineMode(){
        return mmkv.decodeBool(BaseConstant.OFFLINE_MODE,false);
    }
    public void removeOffLineMode(){
        mmkv.remove(BaseConstant.OFFLINE_MODE);
    }

    /**
     * 当前工作模式
     * 离线/在线 false/true
     * @param offLine
     */
    public void currentMode(boolean offLine){
        mmkv.encode(BaseConstant.CURRENT_MODE, offLine);
    }
    public boolean getCurrentMode(){
        return mmkv.decodeBool(BaseConstant.CURRENT_MODE,true);
    }
    public void removeCurrentMode(){
        mmkv.remove(BaseConstant.CURRENT_MODE);
    }
}
